package view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;

import javax.swing.JComponent;

import ressources.Constant;

import modelImp.Player;
import modelImp.Position;
import modelImp.Tile;

public class TileComponent extends JComponent {

	private static final long serialVersionUID = -5432874055485836728L;
	private Tile tile;
	public boolean debug =true;
	public boolean showPosition=false;
	
	
	public TileComponent(Tile t){
		super();
		this.setLayout(null);
		this.setSize(78,78);
		this.setTile(t);
		this.setVisible(true);
		this.setEnabled(true);
		this.initPositions();
	}
	
	public void initPositions() {
		for(int i=0;i<this.tile.getPosition().size();i++){
			if(this.tile.getPosition().get(i).canPutPawn()){
				PositionPoint pospoint = new PositionPoint((Position) this.tile.getPosition().get(i));
				pospoint.setLocation(this.tile.getPosition().get(i).getPoint());
				this.add(pospoint);
			}
		}
	}

	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2d = (Graphics2D)g;
		super.paintComponent(g);
		g2d.drawImage(tile.getImage(),0,0,78,78,null);
		
		/*if(showPosition){
			for(int i=0;i<tile.getPosition().size();i++){
				Position position = (Position) tile.getPosition().elementAt(i);
				if(position.canPutPawn())
				{
					g.fillRect(position.getPoint().x, position.getPoint().y, 5, 5);
				}			
			}
		}*/
		
		if(debug){
			g2d.setColor(Color.black);
			if(tile.getConstraint(0, 0).getPlayers().size()!=0)
				g2d.drawString(tile.getConstraint(0, 0).getPlayers().get(0).getName().toString().substring(0,1),10, 0+10);
			if(tile.getConstraint(0, 1).getPlayers().size()!=0)
				g2d.drawString(tile.getConstraint(0, 1).getPlayers().get(0).getName().toString().substring(0,1), 35, 0+10);
			if(tile.getConstraint(0, 2).getPlayers().size()!=0)
				g2d.drawString(tile.getConstraint(0, 2).getPlayers().get(0).getName().toString().substring(0,1), 60, 0+10);
			
			// S
			if(tile.getConstraint(1,0).getPlayers().size()!=0)
				g2d.drawString(tile.getConstraint(1, 0).getPlayers().get(0).getName().toString().substring(0,1), 10, 78-5);
			if(tile.getConstraint(1,1).getPlayers().size()!=0)
				g2d.drawString(tile.getConstraint(1, 1).getPlayers().get(0).getName().toString().substring(0,1), 35,78-5);
			if(tile.getConstraint(1, 2).getPlayers().size()!=0)
				g2d.drawString(tile.getConstraint(1, 2).getPlayers().get(0).getName().toString().substring(0,1), 60,78-5);
			
			//E
			if(tile.getConstraint(2, 0).getPlayers().size()!=0)
				g2d.drawString(tile.getConstraint(2, 0).getPlayers().get(0).getName().toString().substring(0,1), 70, 15);
			if(tile.getConstraint(2, 1).getPlayers().size()!=0)
				g2d.drawString(tile.getConstraint(2, 1).getPlayers().get(0).getName().toString().substring(0,1), 70, 40);
			if(tile.getConstraint(2, 2).getPlayers().size()!=0)
				g2d.drawString(tile.getConstraint(2, 2).getPlayers().get(0).getName().toString().substring(0,1), 70, 78-10);
			
			//W
			if(tile.getConstraint(3, 0).getPlayers().size()!=0)
				g2d.drawString(tile.getConstraint(3, 0).getPlayers().get(0).getName().toString().substring(0,1), 0, 15);
			if(tile.getConstraint(3, 1).getPlayers().size()!=0)
				g2d.drawString(tile.getConstraint(3, 1).getPlayers().get(0).getName().toString().substring(0,1), 0, 40);
			if(tile.getConstraint(3, 2).getPlayers().size()!=0)
				g2d.drawString(tile.getConstraint(3, 2).getPlayers().get(0).getName().toString().substring(0,1), 0, 68);
			
/*			// N
			g2d.drawString(tile.getConstraint(0, 0).getName().toString().substring(0,1),10, 0+10);
			g2d.drawString(tile.getConstraint(0, 1).getName().toString().substring(0,1), 35, 0+10);
			g2d.drawString(tile.getConstraint(0, 2).getName().toString().substring(0,1), 60, 0+10);
			
			// S
			g2d.drawString(tile.getConstraint(1, 0).getName().toString().substring(0,1), 10, 78-5);	
			g2d.drawString(tile.getConstraint(1, 1).getName().toString().substring(0,1), 35,78-5);
			g2d.drawString(tile.getConstraint(1, 2).getName().toString().substring(0,1), 60,78-5);
			
			//E
			g2d.drawString(tile.getConstraint(2, 0).getName().toString().substring(0,1), 70, 15);
			g2d.drawString(tile.getConstraint(2, 1).getName().toString().substring(0,1), 70, 40);
			g2d.drawString(tile.getConstraint(2, 2).getName().toString().substring(0,1), 70, 78-10);
			
			//W
			g2d.drawString(tile.getConstraint(3, 0).getName().toString().substring(0,1), 0, 15);
			g2d.drawString(tile.getConstraint(3, 1).getName().toString().substring(0,1), 0, 40);
			g2d.drawString(tile.getConstraint(3, 2).getName().toString().substring(0,1), 0, 68);
	*/
		}
		
	}
	
	@Override
	public void setLocation(int x, int y) {
		super.setLocation(x-(x%Constant.WIDTH_TILE), y-(y%Constant.WIDTH_TILE));
		
	}

	public void setTile(Tile tile) {
		this.tile = tile;
	}

	public Tile getTile() {
		return tile;
	}

	public void rotate() {
		tile.rotate();
		this.removeAll();
		this.initPositions();
	}
	/*
	
	public void removePositionsExcept(PositionPoint p){
		for(int i=0;i<this.getComponentCount();i++){
			if(this.getComponent(i) instanceof PositionPoint && this.getComponent(i)!=p) 
			{
				this.remove(i);
			}
		}
	}*/
	public void removePositions(){
		for(int i=0;i<this.getComponentCount();i++){
			if(this.getComponent(i) instanceof PositionPoint)
			{
				PositionPoint pp = (PositionPoint) this.getComponent(i);
				if(pp.position==null || pp.position.getPawn()==null)
					this.remove(i);
			}
		}
	}
	

	public class PositionPoint extends JComponent{
		public Position position=null;
		public PositionPoint(Position p){
			super();
			this.position = p;
			this.setSize(10,10);
			this.setBackground(Color.BLACK);
			this.setVisible(true);
			this.setEnabled(true);
		}
		
		protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.setColor(Color.BLACK);
			g.fillRect(0, 0, this.getWidth(), this.getHeight());
			
			g.setColor(Color.YELLOW);
			if(this.position.getPawn() != null){
				g.fillRect(0, 0, this.getWidth(), this.getHeight());
			}
		}
		
		
		
		
		
	}
}
